package net.togogo.sqlinject;

import com.baomidou.mybatisplus.entity.TableInfo;
import com.baomidou.mybatisplus.mapper.AutoSqlInjector;
import org.apache.ibatis.builder.MapperBuilderAssistant;
import org.apache.ibatis.mapping.SqlSource;
import org.apache.ibatis.session.Configuration;

//自定义全局操作
public class Inject extends AutoSqlInjector {
    //扩展Inject方法，完成自定义的全局操作

    @Override
    public void inject(Configuration configuration, MapperBuilderAssistant builderAssistant, Class<?> mapperClass, Class<?> modelClass, TableInfo table) {
        //将UserMapper中定义的deleteAll,处理成对应的MappedStatement对象，加入到configuration对象中；
        //注入sql语句
        String sql = "delete from " + table.getTableName();
        //注入方法名字 需要与UserMapper接口中方法一致
        String method = "deleteAll";
        //构造sqlsource对象
        SqlSource sqlSource = languageDriver.createSqlSource(configuration,sql,modelClass);
        //构造一个删除的MappedStatement
        this.addDeleteMappedStatement(mapperClass,method,sqlSource);
    }
}
